import Vue from 'vue'
import Vuex, { createLogger } from 'vuex'
import User from "@/api/User.js"
const user = new User();

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 1.登陆状态：id，userName，roleName，token
    // 此处要将所有的state数据存到Session中，否则会引起页面刷新后数据丢失
    id: window.sessionStorage.getItem('id')||"",
    userNickName: window.sessionStorage.getItem('userNickName')||"",
    roleName:window.sessionStorage.getItem('roleName')||"",
    token: window.sessionStorage.getItem('token')||"",
    newsCategories: [],
  },
  getters: {


  },
  mutations: {

    // 初始化数据：NewsCategory
    InitializeData(state,newsCategories){
      state.newsCategories=newsCategories;
      // window.sessionStorage.setItem('newsCategories',newsCategories);
    },

    // 登录成功
    LoginSuccessed(state, { id, roleName, token, userNickName }) {
      // 执行更新数据
      state.id = id; 
      state.roleName = roleName; 
      state.token = token; 
      state.userNickName = userNickName;

      window.sessionStorage.setItem('id',id);
      window.sessionStorage.setItem('roleName',roleName);
      window.sessionStorage.setItem('userNickName',userNickName);
      window.sessionStorage.setItem('token',token);


    },
    // 登录失败
    LoginFailed(state) {
      //  将所有User信息置空
      state.id = ""; 
      state.roleName = ""; 
      state.token = "";
      state.userNickName = "";
      window.sessionStorage.clear();//清空所有Session
    }

  },
  actions: {
    //登录
    async Login(context, { userName, password, isRememberMe }) {
      //  执行在User中已经封装好的axios操作
      const response = (await user.Login(userName, password, isRememberMe))
      const responseData = response.data;
      // 登录成功,再执行mutation更新state中的对应数据
      if (responseData.code === 200) {
        context.commit("LoginSuccessed", {
          id: responseData.data.userId,
          roleName: responseData.data.roleName,
          token: responseData.data.token,
          userNickName: responseData.data.userNickName
        });
        return true
      }
      else {
        // 登录失败
        context.commit("LoginFailed")
        return false;
      }
    },
    //注册
    async Register(context, {
      userName, //用户名
      password, //密码
      confirmPassword, //确认密码
    }) {
      console.log(userName);
      //  执行在User中已经封装好的axios操作
      const response = (await user.Register({ userName, password }))
      const responseData = response.data;
      // 注册成功,再执行mutation更新state中的对应数据
      if (responseData.code === 200) {
        context.commit("LoginSuccessed", {
          id: responseData.data.userId,
          roleName: responseData.data.roleName,
          token: responseData.data.token,
          userNickName: responseData.data.userNickName
        });
        return true
      }
      else {
        // 注册失败
        context.commit("LoginFailed")
        return false;
      }
    },
    //登出
    async Logout(context) {
      var token = window.sessionStorage.getItem('token')
      const response = (await user.Logout(token))
      //  清除token
      if (response) {
        // 清除所有的state
        context.commit("LoginFailed")
      }
    }
  },
  modules: {
  }
})
